package com.bootdo.system.service.impl;

import com.bootdo.common.utils.MyUtils;
import com.bootdo.system.domain.CategoryDO;
import com.bootdo.system.domain.ProductDO;
import com.bootdo.system.domain.SonsysDO;
import com.bootdo.system.service.CategoryService;
import com.bootdo.system.service.ProductService;
import com.bootdo.system.service.SonsysService;
import com.bootdo.system.vo.ProjectVo;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.bootdo.system.dao.ProductInnerDao;
import com.bootdo.system.domain.ProductInnerDO;
import com.bootdo.system.service.ProductInnerService;

import javax.servlet.http.HttpServletResponse;


@Service
public class ProductInnerServiceImpl implements ProductInnerService {
	@Autowired
	private ProductInnerDao productInnerDao;
	@Autowired
	private ProductService productService;
	@Autowired
	private SonsysService sonsysService;
	@Autowired
	private CategoryService categoryService;


	public List<ProjectVo> downExcelWithSonsys(List<ProductInnerDO> productInnerList){

        List<ProjectVo> result = new ArrayList<>();
        if(productInnerList.size()<1){
            return null;
        }

        int tip = productInnerList.get(0).getSonsysId();

        for (ProductInnerDO productInnerDO : productInnerList) {
            ProjectVo projectVo = new ProjectVo();

            ProductDO productDO = productService.get(productInnerDO.getProductId());
            productInnerDO.setProductDO(productDO);
            SonsysDO sonsysDO = sonsysService.get(productInnerDO.getSonsysId());
            productInnerDO.setSonsysDO(sonsysDO);

            BeanUtils.copyProperties(productInnerDO,projectVo);

            projectVo.setShebeiChengben(productDO.getShebeiChengben());
            projectVo.setSonsysName(sonsysDO.getSonsysName());

            Integer sonsysId = sonsysDO.getSonsysId();
            if(sonsysId==tip){
                result.add(projectVo);
            }else{
                ProjectVo sonsysprojectVo = new ProjectVo();
                sonsysprojectVo.setSonsysName(sonsysDO.getSonsysName()+"  合计：");
                sonsysprojectVo.setAllchengben(sonsysDO.getSonchengben());
                sonsysprojectVo.setAllbaojia(sonsysDO.getSonbaojia());
                sonsysprojectVo.setAlllirun(sonsysDO.getSonlirun());
                result.add(sonsysprojectVo);
                tip = sonsysId;
            }


        }



        return result;
    }

    @Override
    public List<Integer> getSonsysListByProjectId(Integer projectId) {
        return productInnerDao.getSonsysListByProjectId(projectId);
    }

	@Override
	public void updateAllProductWithProjectId(ProductInnerDO productInner) {
		productInnerDao.updateAllProductWithProjectId(productInner);
	}



	@Override
	public ProductInnerDO get(Integer id){
		ProductInnerDO productInnerDO = productInnerDao.get(id);

		ProductDO productDO = productService.get(productInnerDO.getProductId());
		productInnerDO.setProductDO(productDO);

		SonsysDO sonsysDO = sonsysService.get(productInnerDO.getSonsysId());
		productInnerDO.setSonsysDO(sonsysDO);

		return productInnerDO;
	}

	@Autowired
	private ProductInnerService productInnerService;
	
	@Override
	public List<ProductInnerDO> list(Map<String, Object> map){
		map.put("sort","sonsys_id");
		List<ProductInnerDO> list = productInnerDao.list(map);
		for (ProductInnerDO productInnerDO : list) {
			Integer sonsysId = productInnerDO.getSonsysId();
			SonsysDO sonsysDO = sonsysService.get(sonsysId == null ? 1 : sonsysId);
			productInnerDO.setSonsysDO(sonsysDO);

			ProductDO productDO = productService.get(productInnerDO.getProductId());
            CategoryDO categoryDO = categoryService.get(productDO.getCategoryId());
            productDO.setCategoryDO(categoryDO);
            productInnerDO.setProductDO(productDO);
            //构建实际报价信息
			double onechengben = productDO.getAnzhuangChengben() + productDO.getShebeiChengben() + productDO.getChengben();
			productInnerDO.setBaojia(MyUtils.getDouble2(onechengben*2));

			double zhekou = productInnerDO.getZhekou();
			productInnerDO.setShijibaojia(MyUtils.getDouble2(productInnerDO.getBaojia()*zhekou));
			productInnerDO.setAllchengben(MyUtils.getDouble2(onechengben*productInnerDO.getProNum()));
			productInnerDO.setAllbaojia(MyUtils.getDouble2(productInnerDO.getShijibaojia()*productInnerDO.getProNum()));
			productInnerDO.setAlllirun(MyUtils.getDouble2(productInnerDO.getAllbaojia()-productInnerDO.getAllchengben()));

			productInnerDO.setProfit(MyUtils.getDouble2(productInnerDO.getAlllirun()/productInnerDO.getAllchengben()*100));

			productInnerService.update(productInnerDO);
		}
		return list;
	}
	
	@Override
	public int count(Map<String, Object> map){
		return productInnerDao.count(map);
	}
	
	@Override
	public int save(ProductInnerDO productInner){


		return productInnerDao.save(productInner);
	}
	
	@Override
	public int update(ProductInnerDO productInner){
		System.out.println("总成本1="+productInner.getAllchengben());
		productInner.setAllchengben(MyUtils.getDouble2(productInner.getAllchengben()));
		productInner.setAllbaojia(MyUtils.getDouble2(productInner.getAllbaojia()));
		productInner.setAlllirun(MyUtils.getDouble2(productInner.getAlllirun()));
		productInner.setProfit(MyUtils.getDouble2(productInner.getProfit()));

		System.out.println("总成本2="+productInner.getAllchengben());

		return productInnerDao.update(productInner);
	}
	
	@Override
	public int remove(Integer id){
		return productInnerDao.remove(id);
	}
	
	@Override
	public int batchRemove(Integer[] ids){
		return productInnerDao.batchRemove(ids);
	}

	@Override
	public List<ProductInnerDO> projectAllMoney(Integer projectId) {
		return productInnerDao.projectAllMoney(projectId);
	}

	@Override
	public List<ProductInnerDO> listall(Integer projectId) {
		return productInnerDao.listall(projectId);
	}

	@Override
	public List<ProductInnerDO> getProDuctBysonsysId(Integer sonsysId) {
		return productInnerDao.getProDuctBysonsysId(sonsysId);
	}

	@Override
	public List<ProductInnerDO> calculateSonsys(Integer projectId, Integer sonsysId) {
		return productInnerDao.calculateSonsys(projectId,sonsysId);
	}



}
